package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.IdCardUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <Description> kite_attr_config校验证件类型 <br>
 *
 * @version 1.0<br>
 * @since R9.0<br>
 */
@Service
public class CheckCardTypeInfoHandler extends AbstractAttrHandler {

    @Resource
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }
    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        KiteAttr cardTypeAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.HANDLER_INFO_CARDTYPE);
        KiteAttr cardNumAttr = querySceneInstService.getCommonAttr(sceneInstId, LocalKeyConsts.HANDLER_INFO_CARDNUM);
        if (KiteStringUtils.isEmpty(value) && KiteStringUtils.isNotEmpty(cardNumAttr.getValue())) {
            return ErrorConsts.SUCCESS.getResult(false);
        }
        if("T".equals(querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE_SCENE_ONE))){
            return ErrorConsts.SUCCESS.getResult(false);
        }
        if (!KiteStringUtils.isEmpty(value)) {
            //经办人证件号码需要根据证件类型的不同进行校验
            if (LocalKeyConsts.HANDLER_INFO_CARDTYPE.equals(fieldName)) {
                if ("5".equals(value)&& KiteStringUtils.isNotEmpty(cardNumAttr.getValue())) {
                    //身份证
                    String msg = IdCardUtils.validate(cardNumAttr.getValue());
                    if (!KiteStringUtils.isEmpty(msg)) {
                        BssException.throwOut("身份证校验", "身份证校验", msg);
                    }
                }
                else if ("7".equals(value)&& KiteStringUtils.isNotEmpty(cardNumAttr.getValue())) {
                    //护照
                    boolean check = StrTools.isPassPortNbr(cardNumAttr.getValue());
                    if (!check) {
                        BssException.throwOut("护照证件号码校验", "护照证件号码校验", "护照证件号码格式错误");
                    }
                }
                else if ("6".equals(value)&& KiteStringUtils.isNotEmpty(cardNumAttr.getValue())) {
                    //军官证
                    boolean check = StrTools.isofficerCard(cardNumAttr.getValue());
                    if (!check) {
                        BssException.throwOut("军官证件号码校验", "军官证件号码校验", "军官证件号码格式错误");
                    }
                }
                else if ("D".equals(value)&& KiteStringUtils.isNotEmpty(cardNumAttr.getValue())) {
                    //港澳居民来往内地通行证
                    boolean check = StrTools.isHKCard(cardNumAttr.getValue());
                    if (!check) {
                        BssException.throwOut("港澳居民来往内地通行证校验", "港澳居民来往内地通行证校验", "港澳居民来往内地通行证格式错误");
                    }
                }
                else if ("E".equals(value)&& KiteStringUtils.isNotEmpty(cardNumAttr.getValue())) {
                    //台湾居民来往大陆通行证
                    boolean check = StrTools.isTWCard(cardNumAttr.getValue());
                    if (!check) {
                        BssException.throwOut("台湾居民来往大陆通行证校验", "台湾居民来往大陆通行证校验", "台湾居民来往大陆通行证格式错误");
                    }
                }
                else if ("C".equals(value)&& KiteStringUtils.isNotEmpty(cardNumAttr.getValue())) {
                    //台湾居民来往大陆通行证
                    boolean check = StrTools.isPoliceCard(cardNumAttr.getValue());
                    if (!check) {
                        BssException.throwOut("警官证校验", "警官证校验", "警官证格式错误");
                    }
                }
                else if(StringUtils.isEmpty(cardTypeAttr.getValue())&& StringUtils.isNotEmpty(cardNumAttr.getValue())){
//                    operateSceneInstService.setCommonAttrValue(sceneInstId, "handler_info_cardnum", "", "");

                }
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

}
